<template>
   <div>
    <!-- 头部标签 -->
    <van-nav-bar fixed>
      <template #left>
        <img class="logo" src="@/assets/images/logo.png" alt="logo" />
      </template>
      <template #right>
        <van-icon name="search" size="0.48rem" color="#fff"  @click="$router.push('/search')"     />
      </template>
    </van-nav-bar>
    <!-- 频道部分 -->
      <van-tabs  animated   sticky
      offset-top="1.226667rem"    v-model=" channelId">
      <van-tab v-for="item in userChannelList" :key="item.id"
       :title="item.name"  :name='item.id' >

        <!-- 内容 -->
        <!-- 上拉加载 -->
         <van-pull-refresh v-model="isLoading" @refresh="pullDownRefreshFn">
        <!-- 上拉加载 -->
       <van-list

         v-model="loading"
        :finished="done"
        finished-text="没有更多了"
        @load="pullUpLoadMoreFn"
        :immediate-check="false"
         offset="50"

        >
    <!-- 每页加载状态 v-model="loading" -->
    <!-- 是否加载完毕:finished="done" -->
    <!-- 结束文本  finished-text="没有更多了"-->
    <!-- 触底事件 @load="pullUpLoadMoreFn" -->
    <!-- 第一次不加载 :immediate-check="false" -->
         <article-list   @dislike="dislikeFn"    :list="articlellist"    />
       </van-list>
           </van-pull-refresh>
       </van-tab>

      </van-tabs>
       <!-- 编辑频道图标+++ -->
      <van-icon name="plus" size="0.45rem"
      class="moreChannels" @click="show=true"    />
       <van-popup v-model="show" class="edit_wrap">
      <!-- 弹出层的主体区域 -->
      <channel-edit  @close-popup='show=false'
      :selecdChannelList= "userChannelList"
      @addChannel='addChannel'
      @del='delChannel'
      @zhanshi='hanfn'
       ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import ChannelEdit from './ChannelEdit.vue'
import { getUserChannelListAPI, getArticlelistAPI, updatedAPT } from '@/api/home'
export default {
  name: 'home',
  data () {
    return {
      show: false, // 编辑频道弹出层显示
      channelId: 1, // 频道ID
      userChannelList: [], // 用户选择的频道列表
      articlellist: [], // 文章列表
      loading: false, //    每一页底部加载状态
      done: false, // 数据是否全部加载完成
      isLoading: false // 顶部刷新状态

    }
  },
  components: { ChannelEdit },
  computed: {
    channels () {
      return this.userChannelList.reduce((sum, obj, index) => {
        const item = {
          id: obj.id,
          seq: index + 1
        }
        if (obj.id !== 0) {
          sum.push(item)
        }
        return sum
      }, [])
    }
  },
  methods: {
    hanfn (id) {
      this.channelId = id
      this.show = false
    },
    // 添加
    addChannel (obj) {
      // 只是vue变了 后台没变
      this.userChannelList.push(obj)
      this.updateChannel()
    },
    // 更新 后台持久化
    updateChannel () {
      updatedAPT({
        channels: this.channels

      })
      this.$notify({ type: 'success', message: '更新' })
    },
    // 删除
    delChannel (index) {
      this.userChannelList.splice(index, 1)
      this.updateChannel()
    },
    // 反馈(不感兴趣)
    dislikeFn (i) {
      this.articlellist.splice(i, 1)
      if (this.articlellist.length <= 6) {
        this.getArticleList()
      }
      this.$notify({ type: 'success', message: 'shan成功' })
    },
    // 底部加载
    pullUpLoadMoreFn () {
      if (this.articlellist.length > 0) {
        this.getArticleList()
      }
    },
    // 上拉
    pullDownRefreshFn () {
      if (this.articlellist.length) return
      this.getArticleList()
    },
    async   getArticleList () {
      // 列表接口
      const resp = await getArticlelistAPI({

        channelId: this.channelId,
        timestamp: this.timestamp

      })
      const { data: { results, pre_timestamp } } = resp
      if (pre_timestamp === null) {
        this.done = true
        this.articlellist = [...this.articlellist, ...results]
        return
      }
      this.articlellist = [...this.articlellist, ...results]

      this.timestamp = pre_timestamp
      this.loading = false
    }
  },
  async created () {
    // 用户选择的频道列表 接口函数

    const res = await getUserChannelListAPI()

    this.userChannelList = res.data.channels
    this.getArticleList()
  },
  watch: {
    channelId () {
      this.timestamp = undefined
      this.articlellist = []
      this.loading = false
      this.done = false

      this.getArticleList()
    }

  },

  activated () {
    scrollTo(0, this.scrollY)
  },
  // deactivated () {
  //   console.log(11)
  // }
  beforeRouteLeave (to, from, next) {
    this.scrollY = window.scrollY
    // console.log(scrollY)
    next()
  }
  // 导航离开该组件的对应路由时调用
  // 可以访问组件实例`this`

  // beforeRouteLeave (to, from, next) {
  //   this.scrollY = scrollY
  // }

}

</script>

<style   scoped lang="less"    >
.logo {
  width: 100px;
  height: 30px;
  }
  /* tab内容距离tab导航的距离 */
  /deep/ .van-tabs__content{
  padding-top: 44px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 20px;
  background-color: #fff;
}
// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 60px;
  right: 8px;
  z-index: 999;
}
/* 频道编辑弹出层铺满屏幕 */
.edit_wrap{
  width: 100%;
  height: 100%;
}
</style>
